const app = getApp();
var njzxw = require('../../utils/njzxw.js');
var url = require('../../utils/url.js');
var tab = require('../../temple/tab/tab.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoContext: null,
    hidevido: true,
    videoSrc: '',
    playSize:0,
    playTitle:'',
    innerAudioContext:null,
    play:false,
    playNum:null,
    payCount:0,
    list:[]
  },
  dianzhan: function (e) {
    var $this = this;
    var id = njzxw.getDataVal(e, 'id');
    var type = njzxw.getDataVal(e, 'type');
    var num = njzxw.getDataVal(e, 'num');
    var res_type = njzxw.getDataVal(e, 'res_type');
    var isDianji = 0;
    if (type == 1) {
      isDianji = $this.data.list[num].is_praise_num;
    } else if (type == 2) {
      isDianji = $this.data.list[num].is_play_num;
    } else {
      isDianji = $this.data.list[num].is_collection_num;
    }
    if (isDianji == 1 && type == 2) {
      return;
    }
    njzxw.ajax({
      url: url.resourcesInfoFront.dianzhan,
      // load: true,
      data: {
        id: id,
        type: type,
        res_type: res_type
      },
      fun: function (a, b) {
        if (a) {
          if (b.data.state == 1) {
            var msg = (type == 1 ? '点赞' : type == 2 ? '播放' : '收藏') + '成功';
            if (isDianji == 1) {
              msg = "取消成功";
            }
            if (type != 2) {
              wx.showToast({
                title: msg,
              })
            }
            var list = $this.data.list;
            if (type == 1) {
              if (isDianji == 1) {
                list[num].praise_num = list[num].praise_num - 1;
                list[num].is_praise_num = 0;
              } else {
                list[num].praise_num = list[num].praise_num + 1;
                list[num].is_praise_num = 1;
              }
            } else if (type == 2) {
              if (isDianji == 1) {
                list[num].play_num = list[num].play_num - 1;
                list[num].is_play_num = 0;
              } else {
                list[num].play_num = list[num].play_num + 1;
                list[num].is_play_num = 1;
              }
            } else {
              if (isDianji == 1) {
                list[num].collection_num = list[num].collection_num - 1;
                list[num].is_collection_num = 0;
              } else {
                list[num].collection_num = list[num].collection_num + 1;
                list[num].is_collection_num = 1;
              }
            }
            $this.setData({
              list: list
            });
          } else {
            njzxw.alert({ msg: b.data.msg });
          }
        } else {
          njzxw.alert({ msg: '操作失败' });
        }
      }
    });
  },
  /**
     * 空值视频播放器的显示
     */
  showVideo: function (e) {
    console.log(e);
    this.hidePay();
    var num = njzxw.getDataVal(e, 'num');
    console.log(num, this.data.list[num]);
    var src = this.data.list[num].res_path;
    if (src == undefined || src == '') {
      src = this.data.list[num].url;
    }
    this.setData({
      hidevido: false,
      videoSrc: src
    });
    this.data.videoContext.play();
    this.dianzhan(e);
  },
  /**
   * 空值视频播放器的显示
   */
  hideVideo: function () {
    this.setData({
      hidevido: true
    });
    this.data.videoContext.pause();
  },
  pausePay: function () {
    this.data.innerAudioContext.pause();
    this.setData({
      play: false
    });
  },
  hidePay: function () {
    if (this.data.innerAudioContext != null) {
      this.data.innerAudioContext.destroy();
      this.data.list[this.data.playNum].play = false;
    }
    // console.log(this.data.playNum, this.data.list[this.data.playNum]);

    this.setData({
      innerAudioContext: null,
      play: false,
      list: this.data.list,
      playNum: null,
      playSize: 0,
      playTitle: ''
    });
  },
  changePay: function (event) {
    // this.data.innerAudioContext.pause();
    var val = event.detail.value;
    this.setData({
      playSize: val
    });
    console.log(val / 100);
    var num = (val / 100) * this.data.payCount;
    console.log(num, this.data.payCount, val);

    this.data.innerAudioContext.seek(num);

    this.data.innerAudioContext.play();
  },
  changePaying: function (event) {
    var val = event.detail.value;
    console.log('==', val);
    this.data.innerAudioContext.pause();
  },
  /**
   * 恢复播放
   */
  hfPlay: function () {
    this.data.innerAudioContext.play();
    this.setData({
      play: true
    });
  },
  showPay: function (e) {
    wx.showLoading({
      title: '缓冲中...',
      mask:true
    });
    wx.setKeepScreenOn({
      keepScreenOn: true
    });
    var $this = this;
    this.hidePay();
    // console.log(this.data.list[e.currentTarget.dataset.num].play);
    var innerAudioContext = wx.createInnerAudioContext();
    innerAudioContext.obeyMuteSwitch = false;
    this.data.list[e.currentTarget.dataset.num].play = true;
    this.setData({
      innerAudioContext: innerAudioContext,
      play: true,
      list: this.data.list,
      playNum: e.currentTarget.dataset.num,
      playTitle: this.data.list[e.currentTarget.dataset.num].res_name
    });
    // console.log(this.data.list[e.currentTarget.dataset.num].play);
    $this.data.innerAudioContext.autoplay = true;

    var src = this.data.list[e.currentTarget.dataset.num].res_path;
    if (src == undefined || src == '') {
      src = this.data.list[e.currentTarget.dataset.num].url;
    }

    $this.data.innerAudioContext.src = src;
    $this.data.innerAudioContext.onError((res) => {
      wx.showModal({
        title: '系统提示',
        content: '音频播放错误'
      })
      $this.setData({
        play: false
      });
    });
    this.data.innerAudioContext.onCanplay(function () {
      console.log(innerAudioContext.duration);
    });
    this.data.innerAudioContext.onEnded(function () {
      $this.setData({
        playSize: 0
      });
      $this.hidePay();
    });
    this.data.innerAudioContext.onTimeUpdate(function () {
      console.log('播放进度：', innerAudioContext.currentTime, innerAudioContext.duration);
      var jindu = parseInt((innerAudioContext.currentTime / innerAudioContext.duration) * 100);
      $this.setData({
        playSize: jindu,
        payCount: innerAudioContext.duration
      });
      wx.hideLoading();
    });
    this.dianzhan(e);
  },
  tapTab:function(e){
    console.log(e);
    wx.redirectTo({
      url: e.target.dataset.url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    njzxw.initFun(this);
    tab.init(this, [
      {
        name: '推荐',
        url: '/pages/shiping/shiping',
        active: true
      },
      {
        name: '视频',
        url: '/pages/shiping/tuijian/tuijian',
        active: false
      },
      {
        name: '音频',
        url: '/pages/shiping/yinping/yinping',
        active: false
      }
    ]);
  },
  
  /**
   * 爱心
   */
  heart:function(){
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.videoContext = wx.createVideoContext('vido');
    this.loadDatas(this);
  },
  loadDatas: function ($this) {
    njzxw.loadStart(this);
    this.loadData(this);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  loadData: function ($this) {
    njzxw.loadData($this, url.resourcesInfoFront.queryPage, {
      pageSize: 20,
      is_tj: 1
    }, function (data) {
      for (var i = 0; i < data.length; i++) {
        data[i]['play'] = false;
      }
      return data;
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.hidePay();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.hidePay();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadDatas(this);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData(this);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})